﻿/// <reference path="../Core.js" />
IM.Utility.ArrayList = function() {
    this.Items = []; //当前list所包含的元素集合
    this.Add = function(obj) {
        if (typeof obj != "undefined")
            this.Items.push(obj);
    };

    this.Remove = function(item) {
        var index = -1;
        for (var i = 0; i < this.Items.length; i++) {
            if (this.Items[i] == item) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            for (var i = index; i < this.Items.length - 1; i++) {
                this.Items[i] = this.Items[i + 1];
            }
            this.Items.pop();
        }
    };

    this.Find = function(index) {
        if (typeof index == "undefined") {
            return null;
        }
        var result = null;
        if (typeof index == "number")
            result = this.Items[index];

        else if (typeof index == "function") {
            for (var i = 0; i < this.Items.length; i++) {
                if (index(this.Items[i])) {
                    result = this.Items[i];
                    break;
                }
            }
        }
        return result;
    };

    this.Count = function() {
        return this.Items.length;
    };

    this.Clear = function() {
        this.Items = [];
    };

    //用指定的方式来比较判断是否包含该对象
    this.Contains = function(obj, compareFun) {
        var result = false;
        for (var i = 0; i < this.Items.length; i++) {
            if (typeof compareFun == "undefined") {
                if (this.Items[i] == obj) {
                    result = true;
                    break;
                }
            }
            else if (compareFun(this.Items[i], obj)) {
                result = true;
                break;
            }
        }
        return result;
    };

    //用指定方法对每一对Item进行处理,fun要有参数 (Item)
    this.Each = function(fun) {
        for (var i = 0; i < this.Items.length; i++) {
            if (fun)
                fun(this.Items[i]);
        }
    };
};